Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: Fix folding and make some performance improvements (Editor) #21669

Merged
merged 33 commits into from
Feb 7, 2024

Conversation

ccordoba12
Copy link
Member

@ccordoba12 ccordoba12 commented Jan 2, 2024

Description of Changes

For folding

  • Fix removing a selection that starts or ends in a folded line (this was totally broken).
  • Remove folded regions when pressing Delete or Backspace in a folded line. I took this idea from VSCode.
  • Update folding decorations in the visible buffer. Before we were doing it in the visible region, but that prevents to quickly show folded regions when scrolling a file.
  • Update folding in a thread. Although this doesn't avoid computing folding in the main thread (due to the GIL), it'll help to do it in an external process (I'll do that in a follow up PR to not make this one bigger).
  • Check that folded regions in the visible buffer are updated correctly by UI inspection. We need to do this because the algorithm to update folding fails sometimes (we'll be able to improve that algorithm after folding is computed in an external process).
  • Make folded lines appear as read-only to users, i.e. users won't be able to edit them. They'll only be able to delete them by pressing the Delete or Backspace keys.
  • Expand test suite to cover the new fixes.

Other improvements

  • Update scroll flags in a thread.
  • Prevent updating scroll flags if the cursor is being moved, e.g. when typing.
  • Avoid computing some unnecessary operations for code snippets.
  • Fix reporting errors that come from PyLSP (this was broken too).

Visual changes

Use chevron icons in the folding panel and move it to be next to the code

The last part preserves the UI we have in Spyder 5, which I think is the right one.

Before After
imagen imagen

Preserve syntax highlighting for folded lines and extend their background color to the full width of the editor

I took this idea from VSCode.

Before After
imagen imagen

Fix style of the folding panel in the light theme

Before After
imagen imagen

Issue(s) Resolved

Part of #21637.

Affirmation

By submitting this Pull Request or typing my (user)name below,
I affirm the Developer Certificate of Origin
with respect to all commits and content included in this PR,
and understand I am releasing the same under Spyder's MIT (Expat) license.

I certify the above statement is true and correct: @ccordoba12

@pep8speaks
Copy link

pep8speaks commented Jan 2, 2024

Hello @ccordoba12! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

Line 133:73: W291 trailing whitespace

Line 349:80: E501 line too long (95 > 79 characters)
Line 350:80: E501 line too long (94 > 79 characters)

Comment last updated at 2024-02-07 18:19:43 UTC

Copy link
Member

@jitseniesen jitseniesen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for keeping the commit history so clean. It meant that I could fairly easily steps through the commit instead of viewing all the hundreds of lines at once.

I have only a couple of minor comments, some general, some specific to the code. I'm happy for you to merge after you have had a look at what I wrote.

  • Pressing Tab on a folded line indents the whole folded region (this is fine), but it also selects the folded region (this is a bit weird). Pressing Shift+Tab un-indents only the first line of the folded region. I don't think this is intentional. It's a bit of a boundary case, though, so not that important.

  • The folding indicator no longer changes colour when you hover above them. That's not a problem as far as I am concerned.

  • Remove folded regions when pressing Delete or Backspace in a folded line. I took this idea from VSCode.

    This means that the editor acts as if the whole folded region is selected if you are on a folded line. The user can't edit a folded line (unless they unfold), they can only delete the whole folded region. I find that surprising, which is not good. On the other hand, I never use folding so I don't have an intuition, and if VSCode does this then the UX is probably not too bad. Thus I'm fine with this.

  • Preserve syntax highlighting for folded lines and extend their background color to the full width of the editor

    The disadvantage is that before it was obvious when a region was folded, but now it is less so. I guess the chevron should be enough of a sign for users.

spyder/plugins/editor/panels/scrollflag.py Outdated Show resolved Hide resolved
spyder/plugins/editor/utils/decoration.py Outdated Show resolved Hide resolved
spyder/plugins/editor/panels/codefolding.py Outdated Show resolved Hide resolved
spyder/plugins/editor/panels/codefolding.py Outdated Show resolved Hide resolved
This prevents freezes in the interface, which was happening before
because half of that computation was done on the main thread.
…esponses

This fixes showing LSP errors on our side, which was broken before (who
knows since when).
This prevents freezing the interface by running that computation on the
main thread.
Now we use a timer to avoid too many calls to that method.
This allows to perform that task on the thread we have for folding,
instead of doing it on the main one.
Also, remove some unused attributes
This was totally broken.
- We need to do this because those operations could have removed some
block decorations.
- This also allows us to call the _on_key_pressed and _expand_selection
methods only when they are needed.
This clears some decorations that are left in the editor when undoing
the removal of unfolded regions.
- This gives more flexibility regarding when to paint those regions.
- It also prevents some glitches in the interface when undoing the
removal of folded regions.
This prevents not painting folded decorations on lines close to the
visible region.
Also, highlight folded blocks when updating decorations in the editor.
This is necessary to decorate blocks not detected as folded by
merge_folding, which are outside of the editor's buffer region.
We're not using them, so that code is not really necessary.
This also fixes the problem that uncollapsed icons were barely visible
on the light theme when hovering the folding panel because they had a
yellow color.
They are the same ones used by VSCode and easier to tell apart.
- Correctly paint the panel on hover for the light theme.
- Remove foreground color and outline for folded blocks and paint their
decorations in full width instead.
Also, use qdebounce instead of a timer to simplify code a bit.
This way we avoid to compute one extra `for` loop while updating folding.
Also, remove an unnecessary block comment.
That avoids an extra operating to get a reference for it in several
places.
@ccordoba12
Copy link
Member Author

Thanks for the review @jitseniesen! About your comments:

Pressing Tab on a folded line indents the whole folded region (this is fine), but it also selects the folded region (this is a bit weird). Pressing Shift+Tab un-indents only the first line of the folded region. I don't think this is intentional. It's a bit of a boundary case, though, so not that important.

I was not aware of that and it was not intended. So, I made that pressing Tab and Shift+Tab in a folded line do nothing.

The user can't edit a folded line (unless they unfold), they can only delete the whole folded region. I find that surprising, which is not good.

The problem is that if we allow editing folded lines, the editor could be left in an inconsistent state in the sense that the new results coming from the LSP could not include the current line as folded. That's why folded lines are like read-only ones.

On the other hand, I never use folding so I don't have an intuition, and if VSCode does this then the UX is probably not too bad. Thus I'm fine with this.

Ok, thanks for the remark.

The disadvantage is that before it was obvious when a region was folded, but now it is less so. I guess the chevron should be enough of a sign for users.

A point in favor of that change is that folded lines are now highlighted to the editor's full width. So you can spot them while moving up and down more easily. And this is also what VSCode does, i.e. it preserves syntax highlighting in folded lines.

@jitseniesen
Copy link
Member

The user can't edit a folded line (unless they unfold), they can only delete the whole folded region. I find that surprising, which is not good.

The problem is that if we allow editing folded lines, the editor could be left in an inconsistent state in the sense that the new results coming from the LSP could not include the current line as folded. That's why folded lines are like read-only ones.

I mean, if I move the cursor to a folded line and then I press for instance x, then the whole folded region is deleted and the letter x is added to the next line. If you want folded lines to be like read-only ones (which I'd agree with), then I'd expect that pressing x on a folded line does nothing.

@ccordoba12
Copy link
Member Author

ccordoba12 commented Feb 6, 2024

I mean, if I move the cursor to a folded line and then I press for instance x, then the whole folded region is deleted and the letter x is added to the next line. If you want folded lines to be like read-only ones (which I'd agree with), then I'd expect that pressing x on a folded line does nothing.

Ok, that's a good point and I also agree with it, so I'll implement it before merging this PR.

That will make appear as read-only to users.
@ccordoba12
Copy link
Member Author

I think this one is finally ready. @jitseniesen, if you find additional issues with folding please open a new issue and I'll address them in a follow-up PR.

@ccordoba12 ccordoba12 merged commit 55e92d7 into spyder-ide:master Feb 7, 2024
14 checks passed
@ccordoba12 ccordoba12 deleted the fix-editor-freezes branch February 7, 2024 19:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants